package com.bawei.home.vm

import androidx.lifecycle.viewModelScope
import com.bawei.common.vm.BaseViewModel
import com.bawei.home.intent.HomeViewIntent
import com.bawei.home.model.HomeRepository
import com.bawei.home.state.HomeUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2025/1/16
 * Desc: 首页ViewModel
 */

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository): BaseViewModel<HomeViewIntent>(){

    private val _uiState = MutableStateFlow<HomeUiState>(HomeUiState.LoadEmpty)
    val uiState: StateFlow<HomeUiState> = _uiState.asStateFlow()

    override fun dispatchIntent(intent: HomeViewIntent) {
        when(intent){
            is HomeViewIntent.LoadVideoType -> loadSimpleVideoType()
            is HomeViewIntent.LoadSimpleVideoListById -> loadSimpleViewById(intent.channelId, intent.page, intent.pageSize)
            is HomeViewIntent.LoadRecommendSimpleVideoList -> loadRecommendSimpleVideo(intent.page, intent.pageSize)
        }
    }

    private fun loadSimpleVideoType(){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.getSimpleTypes().collect{ list ->
                    if (list.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleTypeSuccess(list)
                    } else{
                        _uiState.value = HomeUiState.LoadSimpleTypeFail("没有分类")
                    }
                }
            } catch (e:Exception){
                e.printStackTrace()
                _uiState.value = HomeUiState.LoadSimpleTypeFail("请求失败！！")
            } finally {
                cancel()
            }
        }
    }


    private fun loadRecommendSimpleVideo(page: Int, pageSize: Int){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.getRecommendVideo(page, pageSize).collect{ list ->
                    if (list.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(list)
                    } else{
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("没有推荐视频")
                    }
                }
            } catch (e:Exception){
                e.printStackTrace()
                _uiState.value = HomeUiState.LoadSimpleVideoFail("请求失败！！")
            } finally {
                cancel()
            }
        }

    }

    private fun loadSimpleViewById(channelId: String, page:Int, pageSize: Int){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.getSimpleVideoByChannelId(channelId, page, pageSize).collect{ list ->
                    if (list.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(list)
                    } else{
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("没有此分类视频")
                    }
                }
            } catch (e:Exception){
                e.printStackTrace()
                _uiState.value = HomeUiState.LoadSimpleVideoFail("请求失败！！")
            } finally {
                cancel()
            }
        }



    }


}